﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.Utils.Win;
using mendian;
using DiaoPaiDaYin;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.门店下单;
using DXApplicationTangche.UC.门店下单.DTO;
using DXApplicationTangche.DTO;
using DXApplicationTangche.service;
using DXApplicationTangche.UC.门店下单.form;
using DXApplicationTangche.UC.销售记录;
using static DXApplicationTangche.UC.门店下单.form.Frm订单一览;
using DXApplicationTangche.UC.客户管理;
using DXApplicationTangche.UC.客户CRM.Service;

namespace DXApplicationTangche.UC.Luna
{
    public partial class FrmM2店铺统一下单 : DevExpress.XtraEditors.XtraForm
    {
        /// <summary>
        /// 订单充血类
        /// </summary>
        private OrderModel orderModel = new OrderModel();

        public enum Enum下单类型 { 服装定制下单, 标准款扫码下单, 标准款无码下单 };
        private List<CustomerInformation> customerInformations = new List<CustomerInformation>();// 量体
        public FrmM2店铺统一下单()
        {
            InitializeComponent();
            //this.initData();    //  测试方法
            this.gridControl订单分录一览.DataSource = this.orderModel.OrderDtos;
        }
        /// <summary>
        /// 新增分录
        /// </summary>
        public void buildOrderModel(Dto定制下单 dto定制下单)
        {
            this.orderModel.buildAddOrderDtos(dto定制下单);
        }
        /// <summary>
        /// 辅料信息注入
        /// </summary>
        /// <param name="dto定制下单"></param>
        /// <param name="dto辅料"></param>
        public void buildOrderModel(Dto定制下单 dto定制下单, StyleAccessoryItemsDTO dto辅料, DXApplicationTangche.model.Enum订单类型 @enum)
        {
            this.orderModel.buildAddOrderDtos(dto定制下单, dto辅料, @enum);
        }
        private void Frm门店统一下单_Load(object sender, EventArgs e)
        {
            this.查询客户();
        }
        private void 查询客户()
        {
            this.searchLookUpEdit1.Properties.DataSource = CRM客户Service.getCustomerInfoByShopId("51");
        }
        private void 查询客户量体(int customerId)
        {
            this.customerInformations.Clear();
            List<CustomerFItDTO> customerFItDTOs = CRM客户Service.getCustomerFitInfoById(customerId);
            foreach (CustomerFItDTO item in customerFItDTOs)
            {
                if (!String.IsNullOrEmpty(item.itemNameCn))
                {
                    this.customerInformations.Add(new CustomerInformation(item.propertyNameCn, item.itemNameCn));
                }
                else
                {
                    this.customerInformations.Add(new CustomerInformation(item.propertyNameCn, item.fitValue));
                }
            }
            this.gridControl2.DataSource = this.customerInformations;
            this.gridView2.RefreshData();
        }
        #region 选择用户
        private void searchLookUpEdit1_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }

        private void searchLookUpEdit1View_Click(object sender, EventArgs e)
        {
            var a = this.searchLookUpEdit1.Properties.View.GetSelectedRows();
            foreach (int rowHandle in a)
            {
                Frm客户.cUSTOMER_ID //  no
                    = Convert.ToInt32(this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "customerId").ToString());
                Frm客户.customer_name //  no
                    = this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                //  量体id、地址id
                Frm客户.customer_countid //  no
                    = Convert.ToInt32(this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "fitId").ToString());
                Frm客户.aDDRESS_ID //  no
                    = Convert.ToInt32(this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "addressId").ToString());
            }
            this.查询客户量体(Frm客户.cUSTOMER_ID);
            this.orderModel = new OrderModel();
            this.refreshGridControl();
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit1.ToolTip = null;
            searchLookUpEdit1.EditValue = null;
        }
        private void searchLookUpEdit1_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = Frm客户.customer_name;
        }
        #endregion

        private void tileBarItem1_ItemClick(object sender, TileItemEventArgs e)
        {
            new FrmM2店铺下单选款式新(this).ShowDialog();
        }

        public void refreshGridControl()
        {
            this.gridControl订单分录一览.DataSource = this.orderModel.OrderDtos;
            this.tileView1.RefreshData();
        }

        private void tileBarItem2_ItemClick(object sender, TileItemEventArgs e)
        {
            //new Frm扫码下单(Enum进出库类型.出库).ShowDialog();
        }

        private void siveorder_Click(object sender, EventArgs e)
        {
            if (Frm客户.cUSTOMER_ID == 0)
            {
                MessageBox.Show("客户为空,请选择客户");
                return;
            }
            foreach (OrderDto orderDto in this.orderModel.OrderDtos)
            {
                orderDto.customerId = Frm客户.cUSTOMER_ID.ToString();
                orderDto.customerCountId = Frm客户.customer_countid.ToString();
                orderDto.customerName = Frm客户.customer_name;
                orderDto.addressId = Frm客户.aDDRESS_ID.ToString();
            }
            new Form销售记录(this.orderModel, Frm客户.customer_name, Enum订单类别.Luna店铺订单).ShowDialog();
        }

        private void tileBarItem5_ItemClick(object sender, TileItemEventArgs e)
        {
            new Frm客户("51").ShowDialog();
            Frm客户.cUSTOMER_ID = 0;
            Frm客户.customer_name = "";
            Frm客户.customer_countid = 0;
            Frm客户.aDDRESS_ID = 0;
            this.查询客户();
        }

        private void 修改客户量体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Frm新建客户量体(Frm客户.customer_countid,this.customerInformations).ShowDialog();
            this.查询客户量体(Frm客户.cUSTOMER_ID);
        }
    }
}
